#include "SceneView.h"
namespace sl
{
	SceneView::SceneView(void)
	{
	}

	SceneView::~SceneView(void)
	{
	}

	void SceneView::SetSceneData( sl::Node * pNode )
	{
		// remove the old data
		m_rCamera->RemoveChild( 0, m_rCamera->GetChildNum() );

		// add the new node
		m_rCamera->AddChild( pNode );
	}


	void SceneView::Render( sl::Camera * pCamera, sl::Node * pSceneData )
	{
		SetCamera( pCamera );
		SetSceneData( pSceneData );
		Update();
		Cull();
		Draw();
	}
	void SceneView::Update()
	{

	}

	void SceneView::Cull()
	{
		// find the visible object and insert the state into the Draw state graph

		if ( !m_rStateGraph )
		{
			m_rStateGraph = new StateGraph();
		}
		m_rStateGraph->Clean();
		
		if ( !m_rNodeVisitor )
		{
			m_rNodeVisitor = new CullNodeVisitor();
		}
		m_rNodeVisitor->SetCurrentCamera( m_rCamera.get() );
		m_rNodeVisitor->SetStateGraph( m_rStateGraph.get() );

		// visit all the object
		m_rCamera->Accept(m_rNodeVisitor.get());
		
	}

	void SceneView::Draw()
	{
		// draw all the draw state graph
		if ( !m_rStateGraph )
		{
			// output the error information
		}

		// sort the all the state base on certain type to keep render efficient
		m_rStateGraph->SortStateGraph();

		if ( !m_rRenderInfo )
		{
			m_rRenderInfo = new RenderInfo();
			m_rRenderInfo->setCamera( m_rCamera.get() );
			m_rRenderInfo->setDrawState( new State() );
		}
		m_rStateGraph->Draw( m_rRenderInfo.get() );
	}
}
